<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/16
 * Time: 13:13
 */

namespace App\Game;

use App\Game\Traits\LgTrait;
use Carbon\Carbon;

class lg extends Game
{
    use LgTrait;


    private $platName = 'lg';


    const API_PLAYER_LOGIN = '/Game/goinGame';
    const API_PLAYER_BALANCE = '/Users/userBalance';
    const API_TRANSFER_IN = '/Transfer/platformTransferToGame';
    const API_TRANSFER_OUT = '/Transfer/platformTransferToGame';
    const API_TRANSFER_CHECK_ORDER = '/Transfer/verifyTransferResults';
    const API_BET_RECORD = '/Game/roundRecord';


    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'plat_type' => 'card',
            'status'=>1
        ];
    }

    /**
     * gameapi
     * 2018/9/11 10:13
     * Administrator
     * getLength
     *
     * @return mixed
     */
    public function getLength()
    {
        return $this->length;
    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * getPre
     * 获取前缀
     * @return mixed
     */
    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2019/1/16 17:31
     * Administrator
     * curlCreateMember
     * 创建账号
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array
     * @throws \Exception
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $request->offsetSet('accountUserName',$accountUserName);
        $result = $this->curlJoinGame($request);
        if ($result['success']) {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => $result['message']];
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * curlGetBalance
     * 获取余额
     * @param $accountUserName
     * @return array|mixed
     */
    public function curlGetBalance($accountUserName)
    {
        $data = [
            'username' => $accountUserName,
        ];
        $output = $this->lgRequest($data, self::API_PLAYER_BALANCE);
        $result = json_decode($output, true);

        if (array_key_exists('code', $result)) {
            if ($result['code'] == 0) {
                return ['success' => true, 'data' => ['balance' => $result['result']['amount'],], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '创建用户失败，返回码：' . $result['msg']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }

    /**
     * gameapi
     * 2018/12/3 16:58
     * Administrator
     * curlTransferIn
     * 账号转入
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array
     * @throws \Exception
     */
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $data = [
            'orderno' => $transferid,
            'type' => 1,
            'username' => $accountUserName,
            'currency' => $price,
        ];
        $output = $this->lgRequest($data, self::API_TRANSFER_IN);
        $result = json_decode($output, true);

        if (array_key_exists('code', $result)) {
            if ($result['code'] === "0") {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败，失败错：' . $result['msg']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:09
     * Administrator
     * curlTransferTo
     * 额度转出
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array|mixed
     */
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $data = [
            'orderno' => $transferid,
            'type' => 2,
            'username' => $accountUserName,
            'currency' => $price,
        ];
        $output = $this->lgRequest($data, self::API_TRANSFER_OUT);
        $result = json_decode($output, true);

        if (array_key_exists('code', $result)) {
            if ($result['code'] === "0") {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败，失败错：' . $result['msg']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlCheckTransfer
     * 检查订单状态
     * @param $request
     * @return array|mixed
     */
    public function curlCheckTransfer($request)
    {
        $data = [
            'orderno' => $request->transferid,
        ];
        $output = $this->lgRequest($data, self::API_TRANSFER_CHECK_ORDER);
        $result = json_decode($output, true);

        if (array_key_exists('code', $result)) {
            if ($result['code'] === "0") {
                return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败，失败错：' . $result['msg']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    public function curlKick($request)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有踢线此功能'];
    }

    /**
     * gameapi
     * 2018/11/29 14:26
     * Administrator
     * curlJoinGame
     * 游戏登录
     * @param $request
     * @return array
     * @throws \Exception
     */
    public function curlJoinGame($request)
    {
        $balanceResult = $this->curlGetBalance($request->accountUserName);
        $balance = $balanceResult['success'] ? $balanceResult['data']['balance'] : 0;

        $data = [
            'gameid'=>$request->gamecode??0,
            'username' => $request->accountUserName,
            'nickname' => $request->accountUserName,
            'requestip' => $request->getClientIp(),
            'currency' => $balance
        ];
        if (isset($request->gameCode)) {
            $data['gameid'] = $request->gameCode;
        }
        $result = $this->lgRequest($data, self::API_PLAYER_LOGIN);
        $result = json_decode($result, true);

        if (array_key_exists('code', $result)) {
            if ($result['code'] == 0) {
                return ['success' => true, 'data' => ['url' => $result['result']['game_address']], 'message' => ''];

            } else {
                return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $result['msg']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }

    /**
     * gameapi
     * 2019/1/28 20:29
     * Administrator
     * curlJoinMobileGame
     * 手机端游戏登录
     * @param $request
     * @return array
     * @throws \Exception
     */
    public function curlJoinMobileGame($request)
    {
        return $this->curlJoinGame($request);
    }

    /**
     * gameapi
     * 2018/11/22 19:22
     * Administrator
     * curlGetBetRecord
     * 获取记录
     * @param Carbon $startTime
     * @param Carbon $endTime
     * @return array
     */
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        return [];
        $data = [];
        $start_time = $startTime->timestamp;
        $end_time = $endTime->timestamp;
        $pageLimit = 500;
        $page = $totalPage = 1;


        do {
            $params = [
                'starttime' => $start_time,
                'endtime' => $end_time,
                'page' => $page,
                'pagesize' => $pageLimit,
            ];

            $result = $this->lgRequest($params, self::API_BET_RECORD);
            $result = json_decode($result, true);

            if (is_array($result)) {
                if (array_key_exists('code', $result)) {
                    if ($result['code'] == 0) {
                        $betRecords = $result['result'];
                        $totalPage = $result['page']['page_count'];
                        if (count($betRecords) >= 1) {
                            $data = array_merge($data, $betRecords);
                        }
                    }
                }
            }
            $page++;
        } while ($page <= $totalPage);

        return $data;
    }

}